package com.zxmzy.scaffold.core.utils

import android.annotation.SuppressLint
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.pm.PackageInfo
import android.content.pm.PackageManager
import android.content.pm.ResolveInfo
import android.graphics.Color
import android.graphics.ColorMatrix
import android.graphics.ColorMatrixColorFilter
import android.graphics.Paint
import android.os.Build
import android.os.Process
import android.text.TextUtils
import android.view.View
import kotlin.system.exitProcess

/**
 * @author zhangxiaoming
 * @date 2023/7/27
 * @description app相关工具类
 */
object AppUtil {
    /**
     * 获取版本名称
     */
    fun getVersionName(context: Context): String {
        return try {
            val manager = context.packageManager
            val info = manager.getPackageInfo(context.packageName, 0)
            info.versionName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            "1.0.0"
        } catch (e: Exception) {
            e.printStackTrace()
            "1.0.0"
        }
    }

    /**
     * 获取版本号
     */
    @Suppress("DEPRECATION")
    fun getVersionCode(context: Context): Long {
        return try {
            val manager = context.packageManager;
            val info = manager.getPackageInfo(context.packageName, 0);
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) {
                return info.longVersionCode
            } else {
                return info.versionCode.toLong()
            }
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            1
        } catch (e: Exception) {
            e.printStackTrace()
            1
        }
    }

    fun getPackageName(context: Context): String {
        return try {
            val manager = context.packageManager
            val info = manager.getPackageInfo(context.packageName, 0)
            info.packageName
        } catch (e: PackageManager.NameNotFoundException) {
            e.printStackTrace()
            ""
        }
    }

    fun exitApp() {
        try {
            Process.killProcess(Process.myPid())
            exitProcess(0)
        } catch (e: Exception) {
            e.printStackTrace()
        }
    }


/**
 * 全局置灰
 * 一般我们都知道图像是由一个一个像素点组成的，而像素点是通过RGBA(红绿蓝，透明度)来控制的，这个是数字方向的。
 * 但是在我们早期的彩色电视机中，我们经常会调整彩电的色调，饱和度和亮度，其中色调就是物体的颜色，饱和度就是颜色的纯度，从0到100%来描述，亮度就是颜色的相对明暗程度。
 * 既然饱和度为0就可以变成灰色，那么我们通过设置ColorMatrix然后给到Paint画笔。
*/
    fun setWindowsViewGray(view: View){
        try {
            val paint = Paint()
            val cm = ColorMatrix()
            cm .setSaturation(0f)
            paint.colorFilter = ColorMatrixColorFilter(cm)
            view.setLayerType(View.LAYER_TYPE_HARDWARE,paint)
        }catch (e:Exception){
            e.printStackTrace()
        }catch (e:Error){
            e.printStackTrace()
        }
    }
    /**
     * 恢复原色
     */
    fun setWindowsViewPrimaryColor(view:View){
        try {
            val paint = Paint()
            val cm = ColorMatrix()
            cm.setSaturation(1f)
            paint.colorFilter = ColorMatrixColorFilter(cm)
            view.setLayerType(View.LAYER_TYPE_HARDWARE,paint)
        }catch (e:Exception){
            e.printStackTrace()
        }catch (e:Error){
            e.printStackTrace()
        }
    }

    @SuppressLint("QueryPermissionsNeeded")
    fun startAPP(context: Context, packageName: String) {
        val packageManager = context.packageManager
        val resolveIntent = Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER)
        resolveIntent.setPackage(packageName)

        val apps: List<ResolveInfo> = packageManager.queryIntentActivities(resolveIntent, 0)
        val ri: ResolveInfo? = apps.iterator().next()
        if (ri != null) {
            val className = ri.activityInfo.name
            val intent = Intent(Intent.ACTION_MAIN).apply {
                addCategory(Intent.CATEGORY_LAUNCHER)
                addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
                component = ComponentName(packageName, className)
            }
            context.startActivity(intent)
        }
    }

    @SuppressLint("QueryPermissionsNeeded")
    fun checkAppInstalled(context: Context, pkgName: String): Boolean {
        if (pkgName.isEmpty()) {
            return false
        }
        val packageManager: PackageManager = context.packageManager
        // 获取所有已安装程序的包信息
        val infos: List<PackageInfo>? = packageManager.getInstalledPackages(0)
        if (infos.isNullOrEmpty()) return false
        for (info in infos) {
            if (TextUtils.equals(pkgName, info.packageName)) {
                return true
            }
        }
        return false
    }
}